package com.youdoneed.community.group.web;


import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.group.entity.Forumgroupinvitationrecord;
import com.youdoneed.community.group.service.ForumgroupinvitationrecordService;
import com.youdoneed.community.group.service.ForumuserapplyjoingrouprecordService;
import com.youdoneed.user.feign.UserFeignClient;
import com.youdoneed.user.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 邀请好友加入小组模块控制器
 * @author lid
 * @date 2017.6.25
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/groupInvitation" , method = {RequestMethod.GET,RequestMethod.POST})
@Api(value = "邀请模块控制器", description = "邀请模块控制器")
public class ForumgroupinvitationrecordController {
	
	private final Logger logger = LoggerFactory.getLogger(ForumgroupinvitationrecordController.class);
	
	@Autowired
	ForumgroupinvitationrecordService forumgroupinvitationrecordService;

	@Autowired
	ForumuserapplyjoingrouprecordService forumuserapplyjoingrouprecordService;
	
	@Autowired
	UserFeignClient userFeignClient;
	
	
	/**
	 * 根据邀请者id获取邀请列表
	 * @param userId
	 * @author lixs
	 * @Date 2017.6.28
	 */
	@RequestMapping(value = "/getInvitationsByUserId", method = {RequestMethod.GET,RequestMethod.POST})
	@ApiOperation(notes = "获取邀请列表", value = "获取邀请列表")
	public ResultMessage<List<Forumgroupinvitationrecord>> getInvitationsByUserId(@ApiParam("用户id")@RequestParam String userId,
																	Integer limit,Integer offset){
		logger.info("获取邀请列表");
		List<Forumgroupinvitationrecord> forumgroupinvitationrecord = forumgroupinvitationrecordService.getInvitationsByUserId(userId, offset, limit);
		if(forumgroupinvitationrecord==null){
			return ResultMessage.newFailure("获取被邀请人id列表失败");
		}
		for(int index=0;index<forumgroupinvitationrecord.size();index++){
			User user = userFeignClient.getUser(forumgroupinvitationrecord.get(index).getUserId()).getData();
			if(null!=user){
				forumgroupinvitationrecord.get(index).setUserNickName(user.getName());
				forumgroupinvitationrecord.get(index).setUserHeadUrl(user.getImageUrl());
			}
		}
		
		return ResultMessage.newSuccess("获取邀请列表成功").setData(forumgroupinvitationrecord);
	}
	/**
	 * 处理邀请
	 * @param groupInvitationRecordId
	 * @param inviteStatus
	 * @author lixs
	 * @Date 2017.6.30
	 */
	@RequestMapping(value = "/manageInvitation", method = {RequestMethod.GET,RequestMethod.POST})
	@ApiOperation(notes = "处理邀请(暂时不用)", value = "处理邀请(暂时不用)")
	public ResultMessage<Integer> manageInvitation(@ApiParam("邀请记录id")@RequestParam String groupInvitationRecordId,
												   @ApiParam("邀请状态 邀请中（10010），同意加入（10011），拒绝加入（10012）")@RequestParam Integer inviteStatus){
		logger.info("处理邀请");
		int result = forumgroupinvitationrecordService.manageInvitation(groupInvitationRecordId, inviteStatus);
		if(result==0){
			return ResultMessage.newFailure("你拒绝了加群邀请");
		}
		return ResultMessage.newSuccess("处理邀请成功").setData(result);
		
	}
	/**
	 * 邀请加入小组
	 * @param userId
	 * @param invitedUserId
	 * @param forumGroupId
	 * @author lixs
	 * @Date 2017.6.30
	 */
	@RequestMapping(value = "/saveApplyJoinGroupRecord", method = {RequestMethod.GET,RequestMethod.POST})
	@ApiOperation(notes = "邀请加入小组", value = "邀请加入小组")
	public ResultMessage<Boolean> saveApplyJoinGroupRecord(@ApiParam("邀请人id")@RequestParam String userId,
														   @ApiParam("被邀请人id")@RequestParam String invitedUserId,
														   @ApiParam("小组id")@RequestParam String forumGroupId,
														   @ApiParam("是否需要审核")@RequestParam boolean isNeedAudit){
		logger.info("邀请加入小组");

		if (isNeedAudit){
			int result = forumuserapplyjoingrouprecordService.saveApplyJoinGroupRecord(invitedUserId, forumGroupId);
			if(result==0){
				return ResultMessage.newSuccess("邀请用户成功");
			}
			return ResultMessage.newSuccess("加入小组成功");

		}else {
			boolean result = forumgroupinvitationrecordService.saveApplyJoinGroupRecord(userId, invitedUserId, forumGroupId);
			if(result==false){
				return ResultMessage.newFailure("邀请用户失败");
			}
			return ResultMessage.newSuccess("邀请用户成功").setData(result);
		}


	}
	/**
	 * 保存邀请记录
	 * @param forumgroupinvitationrecord 
	 * @author lixs 
	 * @Date 2017.7.1
	 */
	@RequestMapping(value = "/saveInvitationrecord", method = {RequestMethod.GET,RequestMethod.POST})
	@ApiOperation(notes = "保存邀请记录", value = "保存邀请记录")
	public ResultMessage<Integer> saveInvitationrecord(Forumgroupinvitationrecord forumgroupinvitationrecord){
		logger.info("保存邀请记录");
		int result =forumgroupinvitationrecordService.saveInvitationrecord(forumgroupinvitationrecord);
		if(result==0){
			return ResultMessage.newFailure("保存记录失败");
		}
		return ResultMessage.newSuccess("保存记录成功").setData(result);
	}
	
}
